package com.itheima.controller;

import com.itheima.pojo.Result;
import com.itheima.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController
public class UploadController {
    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;

    // private static final String UPLOAD_DIR = "D:\\javaHeima\\code\\javaweb\\tlias-web-management\\images\\";

    /**
     * 上传文件 - 参数名file
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile file) {
        log.info("文件上传：{}", file.getOriginalFilename());
        try {
            String url = aliyunOSSOperator.upload(file.getBytes(), file.getOriginalFilename());
            //返回的地址
            return Result.success(url);
        } catch (Exception e) {
            log.error("上传文件失败", e);
            return Result.error("上传文件失败");
        }
    }
}



/*            log.info("文件上传: {} {} {}", file, username, age);
            String filename = file.getOriginalFilename();
            String extension = filename.substring(filename.lastIndexOf("."));
            String uuid = UUID.randomUUID().toString().replace("-","");
            String newFilename = uuid + extension;

            File dir = new File(UPLOAD_DIR);
            if (!dir.exists()){
                dir.mkdirs();
            }

        try {
            // 保存文件到 这path目录下得这个文件newFilename
            file.transferTo(new File(UPLOAD_DIR,newFilename));
        } catch (IOException e) {
            //error记录异常信息
            log.error("上传文件失败",e);
            return Result.error("上传文件失败");
        }
        return Result.success();*/